package com.api.service.impl;

import com.api.core.model.WebInitModel;
import com.api.entity.MenuModel;
import com.api.entity.SystemMenu;
import com.api.dao.SystemMenuMapper;
import com.api.service.ISystemMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统菜单表 服务实现类
 * </p>
 *
 * @author huzhaoxi
 * @since 2021-01-11
 */
@Service
public class SystemMenuServiceImpl extends ServiceImpl<SystemMenuMapper, SystemMenu> implements ISystemMenuService {

    @Resource
    private SystemMenuMapper menuMapper;

    @Override
    public WebInitModel menu() {

//        Map<String, Object> map = new HashMap<>(16);
//        Map<String,Object> home = new HashMap<>(16);
//        Map<String,Object> logo = new HashMap<>(16);
//        map.put("menuInfo", JSONArray.parseArray(JSON.toJSONString(getMenu())));
//        home.put("title","首页");
//        home.put("href","../page/welcome-1");//控制器路由,自行定义
//        logo.put("title","后台管理系统");
//        logo.put("image","../images/back.jpg");//静态资源文件路径,可使用默认的logo.png
//        map.put("homeInfo", "{title: '首页',href: 'https://www.baidu.com/'}}");
//        map.put("logoInfo", "{title: 'RUGE ADMIN',image: '../images/logo.png'}");


        Map<String,Object> homeInfo = new HashMap<>(16);
        homeInfo.put("title","链接跳转管理");
        homeInfo.put("href","web/linkManage");//控制器路由,自行定义
        Map<String,Object> logoInfo = new HashMap<>(16);
        logoInfo.put("title","后台管理系统");
        logoInfo.put("image","images/logo.png");//静态资源文件路径,可使用默认的logo.png
        logoInfo.put("href","/");//控制器路由,自行定义



        WebInitModel model = new WebInitModel();
        model.setMenuInfo(getMenu());
        model.setHomeInfo(homeInfo);
        model.setLogoInfo(logoInfo);
        return model;
    }


    private List<MenuModel> getMenu() {
        List<MenuModel> menuList = menuMapper.getMenuList();
        return getChildMenu(menuList, 0);
    }


    private List<MenuModel> getChildMenu(List<MenuModel> list,Integer pid) {
        List<MenuModel> outList = new ArrayList<>();
        for (MenuModel menuModel : list) {
            if (null!=menuModel.getPid()&&pid.toString().equals(menuModel.getPid().toString())){
                menuModel.setChild(getChildMenu(list,menuModel.getId()));
                outList.add(menuModel);
            }

        }
        return outList;
    }


}
